/* Example codes. Show framebuffer information, and do mmap. */

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
	struct fb_fix_screeninfo fb_fix_infos;
	struct fb_var_screeninfo fb_var_infos;
  	int fb_fd = open("/dev/fb0", O_RDWR) ;
  	if (fb_fd < 0) {
  		puts("Cannot open /dev/fb0");
  		exit(1);
    	}
  
  	ioctl(fb_fd, FBIOGET_FSCREENINFO, &fb_fix_infos);
   	ioctl(fb_fd, FBIOGET_VSCREENINFO, &fb_var_infos);
   	char *fb_ptr = (char*)mmap(NULL, fb_fix_infos.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);
   	
   	printf(" Framebuffer:\n smem_len=%i\n", fb_fix_infos.smem_len);
   	printf(" xres=%i\n yres=%i\n bits_per_pixel=%i\n", fb_var_infos.xres, fb_var_infos.yres, fb_var_infos.bits_per_pixel);
	printf("red %d %d green %d %d blue %d %d transp %d %d\n", 
		fb_var_infos.red.offset, fb_var_infos.red.length, 
		fb_var_infos.green.offset, fb_var_infos.green.length, 
		fb_var_infos.blue.offset, fb_var_infos.blue.length,
		fb_var_infos.transp.offset, fb_var_infos.transp.length
		);
	printf(" mmapped framebuffer: %p\n", fb_ptr);
   	
	munmap(fb_ptr, fb_fix_infos.smem_len);
	close(fb_fd);

	return 0;
}
